package Heap;

import java.util.*;

class Student{
    private int age;
    private String name;
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        //判断是不是同一个引用
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }
}
class ageComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return (o1.getAge()-o2.getAge());
    }
}
class nameComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
}


public class TestDemo {
//查找和最小的k对数字
public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
    PriorityQueue<List<Integer>> queue = new PriorityQueue<>(k, (o1, o2)->{
        return (o2.get(0) + o2.get(1)) - (o1.get(0) + o1.get(1));
    });
    //取最小值是为了防止两个数组一个比较少的时候【1】  【1,2,3】
    for(int i = 0; i < Math.min(nums1.length, k); i++){
        for(int j = 0; j < Math.min(nums2.length, k); j++){
            if(queue.size() < k) {
                List<Integer> pair = new ArrayList<>();
                pair.add(nums1[i]);
                pair.add(nums2[j]);
                queue.add(pair);
            }else {
                int top = queue.peek().get(0) + queue.peek().get(1);
                //大于K就出队列
                if(top > nums1[i]+nums2[j]){
                    List<Integer> pair = new ArrayList<>();
                    queue.poll();
                    pair.add(nums1[i]);
                    pair.add(nums2[j]);
                    queue.add(pair);
                }
            }
        }
    }
    List<List<Integer>> res = new LinkedList<>();
    for(int i =0; i < k && !queue.isEmpty(); i++){
        res.add(queue.poll());
    }
    return res;
}
    //topk问题
    public static void topk(int[] array,int k){
        if(array==null) return;
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //o1-o2小根堆
                //o2-o1大根堆
                return o2-o1;
            }
        });
        for (int i = 0; i <array.length ; i++) {
            if(priorityQueue.size()<k){
                priorityQueue.offer(array[i]);
            }else {
                int root=priorityQueue.peek();
                if(root>array[i]){
                    priorityQueue.poll();
                    priorityQueue.offer(array[i]);
                }
            }
        }
        System.out.println(priorityQueue);

    }
    public static void main(String[] args) {
        int[] array={1,7,11};
        int[] array2={2,4,6};
        //用小根堆找最大三个数据
        //用大根堆找最小三个数据

    }
    public static void main1(String[] args) {
        PriorityQueue<Student> priorityQueue=new PriorityQueue<>(new  ageComparator());
        priorityQueue.offer(new Student(23,"海绵宝宝"));
        priorityQueue.offer(new Student(17,"珊迪"));
        System.out.println(priorityQueue);
        List<Integer> a=new ArrayList<>();

    }
}
